<?php
/**
 * User: 未央-请保留版权
 * Agreement: 禁止使用本软件（系统）用于任何违法违规业务或项目,造成的任何法律后果允由使用者（或运营者）承担
 * Date: 2021/3/3
 * Time: 14:34
 */
namespace app\pay\controller;
use think\Db;
use think\Controller;

class Index extends Controller
{
    
    public function getReturn($code = 1, $msg = "成功", $data = null)
    {
        return array("code" => $code, "msg" => $msg, "data" => $data);
    }
    
    public function cehsald()//监控支付回调
    {
       $user = input('user');
       if (!$user || $user == "") {
            $this->success('404',null,'user（附加信息：请输入管理员账号）参数错误');
       }
       $data = Db::name("pay_user")->where("user", $user)->where("type", 2)->find();
       if (!$data) {
            $this->success('404',null,'user（附加信息：输入管理员账号不正确）');
       }
       $this->Heart('cehsald');//监控回调心跳检测
       $rows = Db::name("pay_order")->where("state", 1)->select();
       foreach ($rows as $row){
            $cache = tohc('Heart');
            if($cache->get('pcyjk')>=date('Y-m-d H:i:s',time())){
                if($row['mid_dm']!='mq_gzfbyjk' and $row['mid_dm']!='mq_gqqyjk' and $row['mid_dm']!='mq_gvxyjk'){//免挂(支付宝)||免挂(Q Q)
                }else{
                  echo  '通道：'.$row['mid_type'].' | 订单号：'.$row['order_id'].' | 状态：'.'PC端免挂[在线]监听中'.'<br>'; //交易失败
                  continue;
                }
            }
            $ree = Db::name("pay_jie")->where("pid", $row['pid'])->where("id",$row['mid_id'])->find();
            $cls = app("\\app\common\controller\\".$ree['game_dm']);//支付通道路径
            $run = $cls->cehsald($row);//支付通道数据
            if($run['code']==1){
                $eer = Db::name("pay_gfg")->where("pid", $row['pid'])->where("mid",$row['mid'])->find();
                if($eer){
                $z_pay =  Db::table('pay_order') ->where("pid", $row['pid'])->where("mid", $eer["mid"])->where("state", 2)->sum("price");//总充值
                $jr_pay =  Db::table('pay_order') ->where("pid", $row['pid'])->where("mid", $eer["mid"])->where("state", 2)->whereTime('create_date', 'today')->sum("price");//今日充值
                $zr_pay =  Db::table('pay_order') ->where("pid", $row['pid'])->where("mid", $eer["mid"])->where("state", 2)->whereTime('create_date', 'yesterday')->sum("price");//昨日充值
                Db::name("pay_gfg")->where("pid", $eer["pid"])->where("mid", $eer["mid"])->update(array("z_pay"=>$z_pay,"jr_pay"=>$jr_pay,"zr_pay"=>$zr_pay));
                }
                $data = Db::name("pay_user")->where("id", $row['pid'])->find();
                $hq = huidiao($eer,$row,$data);//推送异步回调数据
                $money = $data['money']-$row['sxf'];
                Db::name("pay_user")->where("id", $data['id'])->update(array("money"=>$money));
                if(success($hq)){
                    Db::name("pay_order")->where("pid", $eer["pid"])->where("id", $row['id'])->update(array("state"=>2,"api_state"=>2,"pay_date"=>time(),"pay_time"=>time()));
                }else{
                    Db::name("pay_order")->where("pid", $eer["pid"])->where("id", $row['id'])->update(array("state"=>2,"api_state"=>3,"pay_date"=>time(),"pay_time"=>time(),"api_jieguo"=>$hq,"api_budan"=>0));
                }
               echo  '通道：'.$run['json']['mid_type'].' | 订单号：'.$run['json']['order_id'].' | 状态：'.$run['data'].'<br>'; //交易成功
            }else{
               echo  '通道：'.$run['json']['mid_type'].' | 订单号：'.$run['json']['order_id'].' | 状态：'.$run['data'].'<br>'; //交易失败
            }
       }
       if(!$rows){
            return '未检测到正在付款的订单';
       }
    }
    
    public function Cookie()//检测系统功能
    {
        jiankong();//检测监控状态
    }
    
    public function notify()//异步回调
    {
      return "success";
    }
    
    public function checkOrder()//支付页面监控
    {
        $orderId = input('orderId');
        if (!$orderId || $orderId == "") {
            return json($this->getReturn(-1, 'orderId（附加信息：请输入订单号（订单号）参数错误'));
        }
        $data = Db::name("pay_order")->where("order_id",$orderId)->find();
        $atad= Db::name("pay_user")->where("id", $data['pid'])->find();
        $eer = Db::name("pay_gfg")->where("pid", $data['pid'])->where("mid",$data['mid'])->find();
        closeEndOrder($data['pid']);//检测过期订单
        $this->zfb_dmf();//支付宝(当面付)回调监控
        jkzfbqq();//云端-QQ-支付宝-监控
        if(!$data){//订单被销毁
            return json($this->getReturn(-1, '该订单已不存在'));
        }
        if($atad['duijei']==1){
            $url = api_fssj($eer,$data,$atad['key']);
            $hq = $data['refer'].$url['return'];
        }else{
            $url = api_yizhifu($eer,$data,$atad['key']);
            $hq = $data['refer'].$url['return'];
        }
        if($data['state']==2){//订单支付成功
            return json($this->getReturn(1, '订单已支付',$hq));
        }else if($data['state']==3){//订单已经超时
            return json($this->getReturn(-1, '订单已过期'));
        }else{
            return json($this->getReturn(0, '订单未支付'));
        }
    }
    
    public function Heart($type)//监控回调心跳检测
    {
        jiankong();//检测监控状态
        
        //$type = input('type');
        if (!$type || $type == "") {
            return json($this->getReturn(-1, "请输入您要监控心跳的类型"));
        }
        
        if($type=='appjkd'){//APP监控端
            $user = input("username");
            if (!$user || $user == "") {
                return json($this->getReturn(-1, "请输入登录账号"));
            }
            $pass = input("password");
            if (!$pass || $pass == "") {
                return json($this->getReturn(-1, "请输入登录密码"));
            }
            $res = Db::name('pay_user')->where('user',$user)->find();
            if ($user != $res["user"]) {
                return json($this->getReturn(-1, "账号或密码错误"));
            }else
            if (!password_verify($pass, $res["pass"])) {
                return json($this->getReturn(-1, "账号或密码错误"));
            }
            $atad = Db::name("pay_user")->where("user", $user)->find();
            if($atad['active']==1 and $atad['type']!=2){
                return json($this->getReturn(-1, "您登录的账号已被禁用，请联系客服咨询原因"));
            }
            $cache = tohc('Heart');
            $cache->set('appjkd',date('Y-m-d H:i:s',time()+60*5),60*5);
            $rows = Db::name("pay_gfg")->where("pid", $atad['id'])->select();
            foreach ($rows as $row){
                $ree = Db::name("pay_jie")->where("pid", $row['pid'])->where("id",$row['typec_id'])->find();
                if($ree['game_dm']=='mq_gzfbjkd'||$ree['game_dm']=='mq_gvxjkd'||$ree['game_dm']=='mq_gqqjkd'||$ree['game_dm']=='gm_gjkd'){//监控端(微信)||监控端(支付宝)||监控端(QQ)||监控端(固码)
                    Db::name("pay_gfg")->where("pid", $row['pid'])->where("typec_id",$ree['id'])->update(array("lastheart"=>time(),"jkstate"=>1));
                }else{
                    continue;
                }
            }
            return json($this->getReturn(1, "心跳正常"));
        }else if($type=='pcyjk'){//PC免挂
            $user = input("username");
            if (!$user || $user == "") {
                return json($this->getReturn(-1, "请输入登录账号"));
            }
            $pass = input("password");
            if (!$pass || $pass == "") {
                return json($this->getReturn(-1, "请输入登录密码"));
            }
            $res = Db::name('pay_user')->where('user',$user)->find();
            if ($user != $res["user"]) {
                return json($this->getReturn(-1, "账号或密码错误"));
            }else
            if (!password_verify($pass, $res["pass"])) {
                return json($this->getReturn(-1, "账号或密码错误"));
            }
            $atad = Db::name("pay_user")->where("user", $user)->find();
            if($atad['active']==1 and $atad['type']!=2){
                return json($this->getReturn(-1, "您登录的账号已被禁用，请联系客服咨询原因"));
            }
            Cookie();//检测Cookie状态
            $cache = tohc('Heart');
            $cache->set('cookie_ip',input('cookie_ip'),60*5);
            $cache->set('ds_todaorul',input('ds_todaorul'),60*5);
            $cache->set('pcyjk',date('Y-m-d H:i:s',time()+60*5),60*5);
            $rows = Db::name("pay_gfg")->select();
            foreach ($rows as $row){
                $ree = Db::name("pay_jie")->where("pid", $row['pid'])->where("id",$row['typec_id'])->find();
                if($ree['game_dm']=='mq_gvxyjk' || $ree['game_dm']=='mq_gzfbyjk' || $ree['game_dm']=='mq_gqqyjk'){//免挂(微信)||免挂(支付宝)||免挂(Q Q)
                    Db::name("pay_gfg")->where("pid", $row['pid'])->where("typec_id",$ree['id'])->update(array("lastheart"=>time(),"jkstate"=>1));
                }else{
                    continue;
                }
            }
            return json($this->getReturn(1, "心跳正常"));
        }else if($type=='cehsald'){//宝塔链接监控回调
            closeEndOrder();//检测过期订单
            $cache = tohc('Heart');
            if($cache->get('pcyjk')<=date('Y-m-d H:i:s',time())){
               $atad = Db::name("pay_sz")->find();
               $cache->set('cookie_ip',$atad['ds_todaorul'],60*5);
               $cache->set('ds_todaorul',$atad['ds_todaorul'],60*5);
               Cookie();//检测Cookie状态
               ydzfbqq();//云端-QQ-支付宝
            }
            $cache->set('cehsald',date('Y-m-d H:i:s',time()+60*5),60*5);
            $rows = Db::name("pay_gfg")->select();
            foreach ($rows as $row){
                $ree = Db::name("pay_jie")->where("pid", $row['pid'])->where("id",$row['typec_id'])->find();
                if($ree['game_dm']!='mq_gzfbjkd'and$ree['game_dm']!='mq_gvxjkd'and$ree['game_dm']!='mq_gvxyjk' and $ree['game_dm']!='gm_gjkd'){//除了监控端(微信)||监控端(支付宝)||免挂(微信)||监控端(固码)
                    Db::name("pay_gfg")->where("pid", $row['pid'])->where("typec_id",$ree['id'])->update(array("lastheart"=>time(),"jkstate"=>1));
                }else{
                    continue;
                }
            }
            return json($this->getReturn(1, "心跳正常"));
        }else{
            return json($this->getReturn(-1, "心跳失败"));
        }
        
    }
    
    public function appdds()//APP监控端推送付款数据
    {
        $user = input("username");
        if (!$user || $user == "") {
            return json($this->getReturn(-1, "请输入登录账号"));
        }
        $pass = input("password");
        if (!$pass || $pass == "") {
            return json($this->getReturn(-1, "请输入登录密码"));
        }
        $game_dm = input("game_dm");
        if (!$game_dm || $game_dm == "") {
            return json($this->getReturn(-1, "请输入通道标识"));
        }
        $type = input("type");
        if (!$type || $type == "") {
            return json($this->getReturn(-1, "请输入付款类型"));
        }
        $money = input("money");
        if (!$money || $money == "") {
            return json($this->getReturn(-1, "请输入付款金额"));
        }
        $res = Db::name('pay_user')->where('user',$user)->find();
        if ($user != $res["user"]) {
            return json($this->getReturn(-1, "账号或密码错误"));
        }else
        if (!password_verify($pass, $res["pass"])) {
            return json($this->getReturn(-1, "账号或密码错误"));
        }
        $atad = Db::name("pay_user")->where("user", $user)->find();
        if($atad['active']==1 and $atad['type']!=2){
            return json($this->getReturn(-1, "您登录的账号已被禁用，请联系客服咨询原因"));
        }
        $rows = Db::name("pay_jie")->where("pid",$res['id'])->where("game_dm",$game_dm)->where("state", 1)->find();
        if (!$rows){
            return json($this->getReturn(-1, "该通道已关闭"));
        }
        $row = Db::name("pay_order")->where("pid",$res['id'])->where("mid_id",$rows['id'])->where("type",$type)->where("price",$money)->where("state", 1)->find();
        $eer = Db::name("pay_gfg")->where("pid", $row['pid'])->where("mid",$row['mid'])->find();
        if($row){
            $eer = Db::name("pay_gfg")->where("pid", $row['pid'])->where("mid",$row['mid'])->find();
            if($eer){
            $z_pay =  Db::table('pay_order') ->where("pid", $row['pid'])->where("mid", $eer["mid"])->where("state", 2)->sum("price");//总充值
            $jr_pay =  Db::table('pay_order') ->where("pid", $row['pid'])->where("mid", $eer["mid"])->where("state", 2)->whereTime('create_date', 'today')->sum("price");//今日充值
            $zr_pay =  Db::table('pay_order') ->where("pid", $row['pid'])->where("mid", $eer["mid"])->where("state", 2)->whereTime('create_date', 'yesterday')->sum("price");//昨日充值
            Db::name("pay_gfg")->where("pid", $eer["pid"])->where("mid", $eer["mid"])->update(array("z_pay"=>$z_pay,"jr_pay"=>$jr_pay,"zr_pay"=>$zr_pay));
            }
            $data = Db::name("pay_user")->where("id", $res['id'])->find();
                $hq = huidiao($eer,$row,$data);//推送异步回调数据
                $money = $data['money']-$row['sxf'];
                Db::name("pay_user")->where("id", $data['id'])->update(array("money"=>$money));
                if(success($hq)){
                    Db::name("pay_tmp")->where("pid", $eer["pid"])->where("order_id",$row['order_id'])->delete();
                    Db::name("pay_order")->where("pid", $eer["pid"])->where("id", $row['id'])->update(array("state"=>2,"api_state"=>2,"pay_date"=>time(),"pay_time"=>time()));
                    return json($this->getReturn(1, $row['order_id'],"回调成功"));
                }else{
                    Db::name("pay_tmp")->where("pid", $eer["pid"])->where("order_id",$row['order_id'])->delete();
                    Db::name("pay_order")->where("pid", $eer["pid"])->where("id", $row['id'])->update(array("state"=>2,"api_state"=>3,"pay_date"=>time(),"pay_time"=>time(),"api_jieguo"=>$hq,"api_budan"=>0));
                    return json($this->getReturn(1, $row['order_id'],"回调失败"));
                }
        }else{
           return json($this->getReturn(-1, "未检测到正在付款的订单"));
        }
    }
    
    public function guanliyuan()//管理员登录
    {
        $user = input("username");
        if (!$user || $user == "") {
            return json($this->getReturn(-1, "请输入登录账号"));
        }
        $pass = input("password");
        if (!$pass || $pass == "") {
            return json($this->getReturn(-1, "请输入登录密码"));
        }
        $res = Db::name('pay_user')->where('user',$user)->find();
        if ($user != $res["user"]) {
            return json($this->getReturn(-1, "账号或密码错误"));
        }else
        if (!password_verify($pass, $res["pass"])) {
            return json($this->getReturn(-1, "账号或密码错误"));
        }
        $atad = Db::name("pay_user")->where("user", $user)->find();
        if($atad['type']!=2){
            return json($this->getReturn(-1, "您登录的账号不是管理员账号"));
        }else{
            return json($this->getReturn(1, "登录成功"));
        }
    }
    
    public function shanghu()//商户登录
    {
        $user = input("username");
        if (!$user || $user == "") {
            return json($this->getReturn(-1, "请输入登录账号"));
        }
        $pass = input("password");
        if (!$pass || $pass == "") {
            return json($this->getReturn(-1, "请输入登录密码"));
        }
        $res = Db::name('pay_user')->where('user',$user)->find();
        if ($user != $res["user"]) {
            return json($this->getReturn(-1, "账号或密码错误"));
        }else
        if (!password_verify($pass, $res["pass"])) {
            return json($this->getReturn(-1, "账号或密码错误"));
        }
        $atad = Db::name("pay_user")->where("user", $user)->find();
        if($atad['active']==1 and $atad['type']!=2){
            return json($this->getReturn(-1, "您登录的账号已被禁用，请联系客服咨询原因"));
        }else{
            return json($this->getReturn(1, "登录成功"));
        }
    }
    
    public function appvxyjk()//APP监控端微信云端推送付款数据
    {
        $user = input("username");
        if (!$user || $user == "") {
            return json($this->getReturn(-1, "请输入登录账号"));
        }
        $pass = input("password");
        if (!$pass || $pass == "") {
            return json($this->getReturn(-1, "请输入登录密码"));
        }
        $game_dm = input("game_dm");
        if (!$game_dm || $game_dm == "") {
            return json($this->getReturn(-1, "请输入通道标识"));
        }
        $type = input("type");
        if (!$type || $type == "") {
            return json($this->getReturn(-1, "请输入付款类型"));
        }
        $money = input("money");
        if (!$money || $money == "") {
            return json($this->getReturn(-1, "请输入付款金额"));
        }
        $res = Db::name('pay_user')->where('user',$user)->find();
        if ($user != $res["user"]) {
            return json($this->getReturn(-1, "账号或密码错误"));
        }else
        if (!password_verify($pass, $res["pass"])) {
            return json($this->getReturn(-1, "账号或密码错误"));
        }
        $atad = Db::name("pay_user")->where("user", $user)->find();
        if($atad['type']!=2){
            return json($this->getReturn(-1, "您登录的账号不是管理员账号"));
        }
        $rows = Db::name("pay_qrcode")->where("game_dm",$game_dm)->where("state", 1)->find();
        if (!$rows){
            return json($this->getReturn(-1, "该通道已关闭"));
        }
        $row = Db::name("pay_order")->where("mid_dm",$rows['game_dm'])->where("type",$type)->where("price",$money)->where("state", 1)->find();
        $eer = Db::name("pay_gfg")->where("mid",$row['mid'])->find();
        if($row){
            $eer = Db::name("pay_gfg")->where("pid", $row['pid'])->where("mid",$row['mid'])->find();
            if($eer){
            $z_pay =  Db::table('pay_order') ->where("pid", $row['pid'])->where("mid", $eer["mid"])->where("state", 2)->sum("price");//总充值
            $jr_pay =  Db::table('pay_order') ->where("pid", $row['pid'])->where("mid", $eer["mid"])->where("state", 2)->whereTime('create_date', 'today')->sum("price");//今日充值
            $zr_pay =  Db::table('pay_order') ->where("pid", $row['pid'])->where("mid", $eer["mid"])->where("state", 2)->whereTime('create_date', 'yesterday')->sum("price");//昨日充值
            Db::name("pay_gfg")->where("pid", $eer["pid"])->where("mid", $eer["mid"])->update(array("z_pay"=>$z_pay,"jr_pay"=>$jr_pay,"zr_pay"=>$zr_pay));
            }
            $data = Db::name("pay_user")->where("id", $row['pid'])->find();
                $hq = huidiao($eer,$row,$data);//推送异步回调数据
                $money = $data['money']-$row['sxf'];
                Db::name("pay_user")->where("id", $data['id'])->update(array("money"=>$money));
                if(success($hq)){
                    Db::name("pay_tmp")->where("pid", $eer["pid"])->where("order_id",$row['order_id'])->delete();
                    Db::name("pay_order")->where("pid", $eer["pid"])->where("id", $row['id'])->update(array("state"=>2,"api_state"=>2,"pay_date"=>time(),"pay_time"=>time()));
                    return json($this->getReturn(1, $row['order_id'],"回调成功"));
                }else{
                    Db::name("pay_tmp")->where("pid", $eer["pid"])->where("order_id",$row['order_id'])->delete();
                    Db::name("pay_order")->where("pid", $eer["pid"])->where("id", $row['id'])->update(array("state"=>2,"api_state"=>3,"pay_date"=>time(),"pay_time"=>time(),"api_jieguo"=>$hq,"api_budan"=>0));
                    return json($this->getReturn(1, $row['order_id'],"回调失败"));
                }
        }else{
           return json($this->getReturn(-1, "未检测到正在付款的订单"));
        }
    }
    
    //V免签监控端心跳检测
    public function appHeart()
    {
        $sign = input("sign");
        if (!$sign || $sign == "") {
            return json($this->getReturn(-1, "请输入通信KEY"));
        }
        $cache = tohc('Heart');
        $cache->set('appjkd',date('Y-m-d H:i:s',time()+60*5),60*5);
        $rows = Db::name("pay_user")->where("active", 0)->select();
        foreach ($rows as $row){
            if ($sign == md5(input("t").$row["key"])) {
                $rows = Db::name("pay_gfg")->where("pid", $row['id'])->select();
                foreach ($rows as $row){
                    $ree = Db::name("pay_jie")->where("pid", $row['pid'])->where("id",$row['typec_id'])->find();
                    if($ree['game_dm']=='mq_gzfbjkd'||$ree['game_dm']=='mq_gvxjkd'){//监控端
                        Db::name("pay_gfg")->where("pid", $row['pid'])->where("typec_id",$ree['id'])->update(array("lastheart"=>time(),"jkstate"=>1));
                    }else{
                        continue;
                    }
                }
            }
        }
        return json($this->getReturn(1, "心跳正常"));
    }
    
    //V免签监控端推送付款数据接口
    public function appPush()
    {
        $sign = input("sign");
        if (!$sign || $sign == "") {
            return json($this->getReturn(-1, "请输入商户KEY"));
        }
        $type = input("type");
        if (!$type || $type == "") {
            return json($this->getReturn(-1, "请输入付款类型"));
        }
        $money = input("price");
        if (!$money || $money == "") {
            return json($this->getReturn(-1, "请输入付款金额"));
        }
        if($type=='1'){
            $type = 'weixin';
            $ros = Db::name("pay_qrcode")->where("game_dm",'mq_gvxjkd')->where("state", 1)->find();
            if (!$ros){
                return json($this->getReturn(-1, "该通道已关闭"));
            }
        }else{
            $type = 'alipay';
            $ros = Db::name("pay_qrcode")->where("game_dm",'mq_gzfbjkd')->where("state", 1)->find();
            if (!$ros){
                return json($this->getReturn(-1, "该通道已关闭"));
            }
        }
        $rows = Db::name("pay_user")->where("active", 0)->select();
        foreach ($rows as $row){
            if ($sign == md5(input("type").input("price").input("t").$row["key"])) {
                $row = Db::name("pay_order")->where("mid_dm",$ros['game_dm'])->where("type",$type)->where("price",$money)->where("state", 1)->find();
                $eer = Db::name("pay_gfg")->where("mid",$row['mid'])->find();
                if($row){
                    $eer = Db::name("pay_gfg")->where("pid", $row['pid'])->where("mid",$row['mid'])->find();
                    if($eer){
                    $z_pay =  Db::table('pay_order') ->where("pid", $row['pid'])->where("mid", $eer["mid"])->where("state", 2)->sum("price");//总充值
                    $jr_pay =  Db::table('pay_order') ->where("pid", $row['pid'])->where("mid", $eer["mid"])->where("state", 2)->whereTime('create_date', 'today')->sum("price");//今日充值
                    $zr_pay =  Db::table('pay_order') ->where("pid", $row['pid'])->where("mid", $eer["mid"])->where("state", 2)->whereTime('create_date', 'yesterday')->sum("price");//昨日充值
                    Db::name("pay_gfg")->where("pid", $eer["pid"])->where("mid", $eer["mid"])->update(array("z_pay"=>$z_pay,"jr_pay"=>$jr_pay,"zr_pay"=>$zr_pay));
                    }
                    $data = Db::name("pay_user")->where("id", $row['pid'])->find();
                    $hq = huidiao($eer,$row,$data);//推送异步回调数据
                    $money = $data['money']-$row['sxf'];
                    Db::name("pay_user")->where("id", $data['id'])->update(array("money"=>$money));
                    if(success($hq)){
                        Db::name("pay_tmp")->where("pid", $eer["pid"])->where("order_id",$row['order_id'])->delete();
                        Db::name("pay_order")->where("pid", $eer["pid"])->where("id", $row['id'])->update(array("state"=>2,"api_state"=>2,"pay_date"=>time(),"pay_time"=>time()));
                        return json($this->getReturn(1, $row['order_id']." 回调成功"));
                    }else{
                        Db::name("pay_tmp")->where("pid", $eer["pid"])->where("order_id",$row['order_id'])->delete();
                        Db::name("pay_order")->where("pid", $eer["pid"])->where("id", $row['id'])->update(array("state"=>2,"api_state"=>3,"pay_date"=>time(),"pay_time"=>time(),"api_jieguo"=>$hq,"api_budan"=>0));
                        return json($this->getReturn(1, $row['order_id']." 回调失败"));
                    }
                }else{
                    return json($this->getReturn(-1, "未检测到正在付款的订单"));
                }
            }
        }
    }
    
    public function zfb_dmf()//支付宝(当面付)回调监控
    {
       $rows = Db::name("pay_order")->where("state", 1)->select();
       foreach ($rows as $row){
            if($row['mid_dm']=='zfb_dmf'){//支付宝(当面付)
            }else{
                continue;
            }
            $ree = Db::name("pay_jie")->where("pid", $row['pid'])->where("id",$row['mid_id'])->find();
            $cls = app("\\app\common\controller\\".$ree['game_dm']);//支付通道路径
            $run = $cls->cehsald($row);//支付通道数据
            if($run['code']==1){
                $eer = Db::name("pay_gfg")->where("pid", $row['pid'])->where("mid",$row['mid'])->find();
                if($eer){
                $z_pay =  Db::table('pay_order') ->where("pid", $row['pid'])->where("mid", $eer["mid"])->where("state", 2)->sum("price");//总充值
                $jr_pay =  Db::table('pay_order') ->where("pid", $row['pid'])->where("mid", $eer["mid"])->where("state", 2)->whereTime('create_date', 'today')->sum("price");//今日充值
                $zr_pay =  Db::table('pay_order') ->where("pid", $row['pid'])->where("mid", $eer["mid"])->where("state", 2)->whereTime('create_date', 'yesterday')->sum("price");//昨日充值
                Db::name("pay_gfg")->where("pid", $eer["pid"])->where("mid", $eer["mid"])->update(array("z_pay"=>$z_pay,"jr_pay"=>$jr_pay,"zr_pay"=>$zr_pay));
                }
                $data = Db::name("pay_user")->where("id", $row['pid'])->find();
                $hq = huidiao($eer,$row,$data);//推送异步回调数据
                $money = $data['money']-$row['sxf'];
                Db::name("pay_user")->where("id", $data['id'])->update(array("money"=>$money));
                if(success($hq)){
                    Db::name("pay_order")->where("pid", $eer["pid"])->where("id", $row['id'])->update(array("state"=>2,"api_state"=>2,"pay_date"=>time(),"pay_time"=>time()));
                }else{
                    Db::name("pay_order")->where("pid", $eer["pid"])->where("id", $row['id'])->update(array("state"=>2,"api_state"=>3,"pay_date"=>time(),"pay_time"=>time(),"api_jieguo"=>$hq,"api_budan"=>0));
                }
            }
       }
    }
    
    public function refer2()
    {
        error_reporting(0);  
        $ata = input();
        if($ata['out_trade_no']){
            $data = Db::name("pay_order")->where("pay_id", $ata['out_trade_no'])->find();//易支付
        }else if($ata['order']){
            $data = Db::name("order")->where("pay_id", $ata['order'])->find();//默认
        }
        $bqdf = Db::name("pay_user")->where("id", $data['pid'])->find();
        $eer = Db::name("pay_gfg")->where("pid", $data['pid'])->where("mid",$data['mid'])->find();
        if(!$data){//订单被销毁
            $this->success('404',null,'该订单已不存在');
        }
        if($bqdf['duijei']==1){
            $url = api_fssj($eer,$data,$bqdf['key']);
            $hq = $data['refer'].$url['return'];
        }else{
            $url = api_yizhifu($eer,$data,$bqdf['key']);
            $hq = $data['refer'].$url['return'];
        }
        if($data['state']==2){//订单支付成功
            return redirect($hq);
        }else if($data['state']==3){//订单已经超时
            $this->success('404',null,'订单已过期');
        }else{
            $this->success('404',null,'订单未支付');
        }
    }
    
    public function notify2()
    {
        error_reporting(0);  
        $ata = input();
        if($ata['out_trade_no']){
            $data = Db::name("pay_order")->where("pay_id", $ata['out_trade_no'])->find();//易支付
        }else if($ata['order']){
            $data = Db::name("order")->where("pay_id", $ata['order'])->find();//默认
        }
        $res = Db::name("pay_user")->where("id", $data['pid'])->find();
        $eer = Db::name("pay_gfg")->where("pid", $data['pid'])->where("mid",$data['mid'])->find();
        if(!$data){//订单被销毁
            $this->success('404',null,'该订单已不存在');
        }
        $rows = Db::name("pay_jie")->where("pid",$res['id'])->where("game_dm",$data['mid_dm'])->where("state", 1)->find();
        if (!$rows){
            return json($this->getReturn(-1, $data['mid_dm']));
        }
        $row = Db::name("pay_order")->where("pid",$res['id'])->where("mid_id",$rows['id'])->where("type",$data['type'])->where("price",$data['money'])->where("state", 1)->find();
        $eer = Db::name("pay_gfg")->where("pid", $row['pid'])->where("mid",$row['mid'])->find();
        if($row){
            $eer = Db::name("pay_gfg")->where("pid", $row['pid'])->where("mid",$row['mid'])->find();
            if($eer){
            $z_pay =  Db::table('pay_order') ->where("pid", $row['pid'])->where("mid", $eer["mid"])->where("state", 2)->sum("price");//总充值
            $jr_pay =  Db::table('pay_order') ->where("pid", $row['pid'])->where("mid", $eer["mid"])->where("state", 2)->whereTime('create_date', 'today')->sum("price");//今日充值
            $zr_pay =  Db::table('pay_order') ->where("pid", $row['pid'])->where("mid", $eer["mid"])->where("state", 2)->whereTime('create_date', 'yesterday')->sum("price");//昨日充值
            Db::name("pay_gfg")->where("pid", $eer["pid"])->where("mid", $eer["mid"])->update(array("z_pay"=>$z_pay,"jr_pay"=>$jr_pay,"zr_pay"=>$zr_pay));
            }
            $data = Db::name("pay_user")->where("id", $res['id'])->find();
                $hq = huidiao($eer,$row,$data);//推送异步回调数据
                $money = $data['money']-$row['sxf'];
                Db::name("pay_user")->where("id", $data['id'])->update(array("money"=>$money));
                if(success($hq)){
                    Db::name("pay_tmp")->where("pid", $eer["pid"])->where("order_id",$row['order_id'])->delete();
                    Db::name("pay_order")->where("pid", $eer["pid"])->where("id", $row['id'])->update(array("state"=>2,"api_state"=>2,"pay_date"=>time(),"pay_time"=>time()));
                    return 'success';
                }else{
                    Db::name("pay_tmp")->where("pid", $eer["pid"])->where("order_id",$row['order_id'])->delete();
                    Db::name("pay_order")->where("pid", $eer["pid"])->where("id", $row['id'])->update(array("state"=>2,"api_state"=>3,"pay_date"=>time(),"pay_time"=>time(),"api_jieguo"=>$hq,"api_budan"=>0));
                    return 'success';
                }
        }else{
            $this->success('404',null,'未检测到正在付款的订单');
        }
    }
    
    public function hxydCk()//免挂(支付宝)||免挂(Q Q)-PC免挂ck数据接口
    {
        $user = input("username");
        if (!$user || $user == "") {
            return json($this->getReturn(-1, "请输入登录账号"));
        }
        $pass = input("password");
        if (!$pass || $pass == "") {
            return json($this->getReturn(-1, "请输入登录密码"));
        }
        $game_dm = input("game_dm");
        if (!$game_dm || $game_dm == "") {
            return json($this->getReturn(-1, "请输入通道标识"));
        }
        $res = Db::name('pay_user')->where('user',$user)->find();
        if ($user != $res["user"]) {
            return json($this->getReturn(-1, "账号或密码错误"));
        }else
        if (!password_verify($pass, $res["pass"])) {
            return json($this->getReturn(-1, "账号或密码错误"));
        }
        $atad = Db::name("pay_user")->where("user", $user)->find();
        if($atad['type']!=2){
            return json($this->getReturn(-1, "您登录的账号不是管理员账号"));
        }
        $rows = Db::name("pay_gfg")->where("state",1)->select();
        $list = array();
        foreach ($rows as $row){
            $ros = Db::name("pay_jie")->where("id", $row['typec_id'])->find();
            $data = Db::name("pay_user")->where("id", $row['pid'])->find();
            if($game_dm=='mq_gzfbyjk'){
                if($ros['game_dm']!='mq_gzfbyjk'){//免挂(支付宝)
                 continue;
                }
            }else
            if($game_dm=='mq_gqqyjk'){
                if($ros['game_dm']!='mq_gqqyjk'){//免挂(Q Q)
                 continue;
                }
            }else{
                continue;
            }
            $list[] = [
                'pid' => $row['pid'],
                'mid' => $row['mid'],
                'username' => $row['username'],
                'Cookie' => $row['ck'],
                ];
        }
        return json($this->getReturn(1, '获取成功',$list));
    }
    
    public function pcvxyjk()//PC端免挂微信推送付款数据
    {
        $user = input("username");
        if (!$user || $user == "") {
            return json($this->getReturn(-1, "请输入登录账号"));
        }
        $pass = input("password");
        if (!$pass || $pass == "") {
            return json($this->getReturn(-1, "请输入登录密码"));
        }
        $game_dm = input("game_dm");
        if (!$game_dm || $game_dm == "") {
            return json($this->getReturn(-1, "请输入通道标识"));
        }
        $type = input("type");
        if (!$type || $type == "") {
            return json($this->getReturn(-1, "请输入付款类型"));
        }
        $mingcheng = input("mingcheng");
        if (!$mingcheng || $mingcheng == "") {
            return json($this->getReturn(-1, "请输入店员名称"));
        }
        $money = input("money");
        if (!$money || $money == "") {
            return json($this->getReturn(-1, "请输入付款金额"));
        }
        $res = Db::name('pay_user')->where('user',$user)->find();
        if ($user != $res["user"]) {
            return json($this->getReturn(-1, "账号或密码错误"));
        }else
        if (!password_verify($pass, $res["pass"])) {
            return json($this->getReturn(-1, "账号或密码错误"));
        }
        $atad = Db::name("pay_user")->where("user", $user)->find();
        if($atad['type']!=2){
            return json($this->getReturn(-1, "您登录的账号不是管理员账号"));
        }
        $rows = Db::name("pay_qrcode")->where("game_dm",$game_dm)->where("state", 1)->find();
        if (!$rows){
            return json($this->getReturn(-1, "该通道已关闭"));
        }
        $eer = Db::name("pay_gfg")->where("zhanghao",DeleteHtml($mingcheng))->find();
        $row = Db::name("pay_order")->where("mid_dm",$rows['game_dm'])->where("type",$type)->where("mid",$eer['mid'])->where("price",$money)->where("state", 1)->find();
        if($row){
            $eer = Db::name("pay_gfg")->where("pid", $row['pid'])->where("mid",$row['mid'])->find();
            if($eer){
            $z_pay =  Db::table('pay_order') ->where("pid", $row['pid'])->where("mid", $eer["mid"])->where("state", 2)->sum("price");//总充值
            $jr_pay =  Db::table('pay_order') ->where("pid", $row['pid'])->where("mid", $eer["mid"])->where("state", 2)->whereTime('create_date', 'today')->sum("price");//今日充值
            $zr_pay =  Db::table('pay_order') ->where("pid", $row['pid'])->where("mid", $eer["mid"])->where("state", 2)->whereTime('create_date', 'yesterday')->sum("price");//昨日充值
            Db::name("pay_gfg")->where("pid", $eer["pid"])->where("mid", $eer["mid"])->update(array("z_pay"=>$z_pay,"jr_pay"=>$jr_pay,"zr_pay"=>$zr_pay));
            }
            $data = Db::name("pay_user")->where("id", $row['pid'])->find();
                $hq = huidiao($eer,$row,$data);//推送异步回调数据
                $money = $data['money']-$row['sxf'];
                Db::name("pay_user")->where("id", $data['id'])->update(array("money"=>$money));
                if(success($hq)){
                    Db::name("pay_tmp")->where("pid", $eer["pid"])->where("order_id",$row['order_id'])->delete();
                    Db::name("pay_order")->where("pid", $eer["pid"])->where("id", $row['id'])->update(array("state"=>2,"api_state"=>2,"pay_date"=>time(),"pay_time"=>time()));
                    return json($this->getReturn(1, $row['order_id'],"回调成功"));
                }else{
                    Db::name("pay_tmp")->where("pid", $eer["pid"])->where("order_id",$row['order_id'])->delete();
                    Db::name("pay_order")->where("pid", $eer["pid"])->where("id", $row['id'])->update(array("state"=>2,"api_state"=>3,"pay_date"=>time(),"pay_time"=>time(),"api_jieguo"=>$hq,"api_budan"=>0));
                    return json($this->getReturn(1, $row['order_id'],"回调失败"));
                }
        }else{
           return json($this->getReturn(-1, "未检测到正在付款的订单"));
        }
    }
    
    //PC免挂推送付款数据接口
    public function pcyjk()
    {
        $user = input("username");
        if (!$user || $user == "") {
            return json($this->getReturn(-1, "请输入登录账号"));
        }
        $pass = input("password");
        if (!$pass || $pass == "") {
            return json($this->getReturn(-1, "请输入登录密码"));
        }
        $game_dm = input("game_dm");
        if (!$game_dm || $game_dm == "") {
            return json($this->getReturn(-1, "请输入通道标识"));
        }
        $json = input("json");
        if (!$json || $json == "") {
            return json($this->getReturn(-1, "请输入监控JSON格式数据"));
        }
        $res = Db::name('pay_user')->where('user',$user)->find();
        if ($user != $res["user"]) {
            return json($this->getReturn(-1, "账号或密码错误"));
        }else
        if (!password_verify($pass, $res["pass"])) {
            return json($this->getReturn(-1, "账号或密码错误"));
        }
        $atad = Db::name("pay_user")->where("user", $user)->find();
        if($atad['type']!=2){
            return json($this->getReturn(-1, "您登录的账号不是管理员账号"));
        }
        $rows = Db::name("pay_qrcode")->where("game_dm",$game_dm)->where("state", 1)->find();
        if (!$rows){
            return json($this->getReturn(-1, "该通道已关闭"));
        }
        $ret = $json;
        $json = json_decode($ret,TRUE); 
        $data = Db::name("pay_user")->where("id", $atad['id'])->find();
        if($json['code']=="1"){
            $cache = tohc($game_dm);//通道缓存
            if($cache->get($json['data']['uid'])==$ret){
                return json($this->getReturn(-1, '监听中'));
            }else{
                $cache->rm($json['data']['uid']);//删除旧缓存
                $cache->set($json['data']['uid'],$ret,60*$data['bug_close']);// 将数据插入新缓存文件中
                jkzfbqq();//云端-QQ-支付宝-监控
                return json($this->getReturn(1, '推送成功'));
            }
        }else{
            return json($this->getReturn(-1, '推送失败'));
        }
    }
    
}